<?php

tsLoad('/core/CoreLib/AbstractMysqlPage.class.php');
class WithDrawPage extends AbstractMysqlPage {
	/*
	 * 取得总的行数
	 */
	protected function calTotalRows (){
		global $mysqlTools,$userSession;
        $condition = $mysqlTools->check_input($this->condition,array('userLoginId'));
        $userId =$userSession->getUserSession()["userId"];
       	$sqlCore = "
       		select count(*) AS totalCount  
       		from 
       		withd_info wi left join sys_user u on wi.withdPassUserId=u.userId	
        ";
        $where = "
         	WHERE 1=1  AND wi.userId=$userId
        ";
        //查看状态
        if(!empty($condition['withdState']) && $condition['withdState']!=4){
        	$where.= ' AND wi.withdState ='.$condition['withdState'];
        }
        
	 	//创建开始时间与结束时间
        if(!empty($condition['withdCreatetimeStart'])){
        	$where.= ' AND wi.withdCreatetimeStart >='.$condition['withdCreatetimeStart'];
        }
        if(!empty($condition['withdCreatetimeEnd'])){
        	$where.= ' AND wi.withdCreatetimeEnd='.$condition['withdCreatetimeEnd'];
        }
        
        $sql = $sqlCore.$where;
        //总记录数
        $totalRows = $mysqlTools->selectSql($sql)[0]['totalCount'];
        
        return $totalRows;
	}
	
	/*
	*取得数据集合
			*/
	protected function getList ($sRowNo, $pageRows)
	{
	  global $mysqlTools,$userSession;
        
        //保存一个原生值以便需要的时候进行处理
        $orgCondition = $this->condition;
        
        $condition = $mysqlTools->check_input($this->condition,array('userLoginId'));
        $userId =$userSession->getUserSession()["userId"];
        $sqlCore = "
				    select 
					   wi.withdId,
					   wi.withdMo,
					   wi.withdCounterMo,
        			   wi.userId,
					   wi.userLoginId,
					   wi.withdTotalMo,
					   wi.withdCreatetime,
					   wi.withdPassTime,
					   wi.withdState,
					   wi.withdPassUserId,
					   wi.withdPassLoginId,
        			   wi.withBankNo,
        			   wi.withBankName,
        			   wi.withBankAccount,
        			   wi.withBankUserName,
					   u.userLoginId	
					from withd_info wi left join sys_user u on wi.withdPassUserId=u.userId
        ";
        
        $where = "
         	WHERE 1=1  AND wi.userId=$userId
        ";
        //查看状态
        if(!empty($condition['withdState']) && $condition['withdState']!=4){
        	$where.= ' AND wi.withdState ='.$condition['withdState'];
        }
        
        //创建开始时间与结束时间
        if(!empty($condition['withdCreatetimeStart'])){
        	$where.= ' AND wi.withdCreatetime >='.$condition['withdCreatetimeStart'];
        }
        if(!empty($condition['withdCreatetimeEnd'])){
        	$where.= ' AND wi.withdCreatetime <='.$condition['withdCreatetimeEnd'];
        }
        
        $order = " ORDER BY wi.withdCreatetime DESC ";
        
        $limit = " LIMIT $sRowNo,$pageRows";
        
        $sql = $sqlCore.$where.$order.$limit;
        
        //查询角色信息分页
        $list = $mysqlTools->selectSql($sql);
        
        return $list;
	}
	//提现
	public function insertWithDraw($condition){
		//判断是否可以提现
		$withDrawPage = new WithDrawPage();
		$balance = $withDrawPage->getbalance();
		//判断该用户的野猪币是否够用  (提现+手续费)-1000>0方可提现
		$m=$condition['withdMo'];
		if($m < 100){
			return '提现金额至少100元!';
		}
		$e=$m*0.05;
		$resultB=($balance-($m+$e))-1000;
		if($m<0){
			return '提现金额输入无效,重新输入!';
		}
		if($resultB<0){
			return '野猪币余额不够本次提现!';
		}
		global $mysqlTools,$userSession;
		$condition['withdTotalMo']=$m+$e;
		//生成唯一ID
		$condition['withdCounterMo']=$e;
		$uniqid = getCurrentId();
		$userRealName = $userSession->getUserSession()['userRealName'];
		if($userRealName!=$condition['withBankUserName']){
			return '持卡人姓名和您注册时的姓名不同!';
		}
		//参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input($condition);
		//处理null值,如果值为null数据库也置为null
		$condition = $mysqlTools->check_null($condition);
		try {
			$userId = $userSession->getUserSession()['userId'];
			$userLoginId = $userSession->getUserSession()['userLoginId'];
			$mysqlTools->startTransaction();
			$result = $mysqlTools->executeSql("
							insert into withd_info 
							(
								withdId, 
								withdMo, 
								withdCounterMo, 
								userId, 
								userLoginId, 
								withdTotalMo, 
								withdCreatetime, 
								withdState,
								withBankName,
								withBankAccount,
								withBankNo,
								withBankUserName
							)
							values
							(
								$uniqid, 
								$condition[withdMo], 
								$condition[withdCounterMo], 
								$userId, 
								'$userLoginId', 
								$condition[withdTotalMo], 
								NOW(), 
								1,
								$condition[withBankName],
								$condition[withBankAccount],
								$condition[withBankNo],
								$condition[withBankUserName]
							)
					");
			//减去用户的野猪币
			$mysqlTools->executeSql("
					update sys_user
					set userGold=userGold-$condition[withdTotalMo]
					where
					userId=$userId
					");
			//添加系统收益明细
			$sysProInId=getCurrentId();
			//添加日志
			//获取角色信息
			$roleSql="select r.roleId,r.roleDesc from sys_user_role s left join sys_role r on s.roleId=r.roleId where s.userId=$userId limit 1";
			$roleId = $mysqlTools->selectSql($roleSql)[0]['roleId'];
			$roleDesc = $mysqlTools->selectSql($roleSql)[0]['roleDesc'];
			$userInFId = getCurrentId();//主键
			$less=$balance-($m+$e);//剩余野猪币
			$mysqlTools->executeSql("
					insert into user_bop_info(
					   userInFId,
					   userInFZf,
					   userInFUserId,
					   userInFLoginId,
					   userInFCreatetime,
					   userInFType,
					   userInFMo,
					   roleId,
					   roleDesc,
					   userInFReason,
					   withdId,
					   sysProInId,
					   userInFSurGlod 
					)values(
					   $userInFId,
					   2,
					   $userId,
					   '$userLoginId',
					   NOW(),
					   6,
					   $condition[withdTotalMo],
					   $roleId,
					   '$roleDesc',
					   8,
					   $uniqid,
					   $sysProInId,
					   $less
					)
					");
			$mysqlTools->commitTransaction();
		} catch (\Exception $e) {
			$mysqlTools->rollback();
		}
		return $result;
	}
	//获取余额
	public function getbalance(){
			global $mysqlTools,$userSession;
			
			$userId = $userSession->getUserSession()['userId'];
			
			$sqlCore = "
				   select
						u.userGold as balance
				   from 
					sys_user u
        	";
			$where = "
				WHERE 1=1  AND u.userId=$userId
			";
			$sql = $sqlCore.$where;
			$balance = $mysqlTools->selectSql($sql)[0]['balance'];
			
			return $balance;
	}
}
?>